<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.5);
            position: absolute;
            left: 100px;
            top: 100px;
        }
    </style>
</head>

<body>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
    <div class="shadow"></div>
</body>
<script>

    function dragEle(ele) {
        var obj = {
            target: ele,
            // maxLeft: null,
            // maxTop: null,
            getBoundary: function () {
                // 相对于文档的边界
                this.maxLeft = document.documentElement.clientWidth - this.target.clientWidth;
                this.maxTop = document.documentElement.clientHeight - this.target.clientHeight;

                // 多次调用getBoundary => 后面覆盖前面的
                // window.onresize = () => {
                //     this.getBoundary();
                // }

                // 可以开启多个事件监听
                window.addEventListener("resize", () => {
                    this.getBoundary();
                })

            },
            dragStart: function () {
                // 开始拖拽  => 给目标元素绑定mousedown事件 -> 记录按下的位置
                // this -> dragEle(字面量对象)

                var _this = this; // 对象的复制 -> 浅复制 (_this -> dragEle)

                this.target.onmousedown = function () {
                    // 事件中的this -> 绑定事件的元素(shadow)
                    var e = e || window.event;

                    var posX = e.offsetX;
                    var posY = e.offsetY;
                    // console.log(posX, posY);  //按下的位置

                    // 将posX  posY -> 存储到dragEle中
                    _this.posX = posX;
                    _this.posY = posY;

                    console.log(_this);

                    // 按下时绑定移动和松开事件
                    _this.dragMove();
                    _this.dragEnd();

                }


            },
            dragMove: function () {
                // 开始移动 -> 目标匀速跟随鼠标移动  在文档(可变)中移动
                // this-> dragEle
                // var _this = this;
                document.onmousemove = (e) => {

                    var e = e || window.event;

                    // 箭头函数没有自己的this指向 -> 依赖于父作用域this指向(dragEle)
                    var x = e.pageX - this.posX;
                    var y = e.pageY - this.posY;
                    console.log(x, y);

                    if (x < 0) x = 0;
                    if (x > this.maxLeft) x = this.maxLeft;

                    if (y < 0) y = 0;
                    if (y > this.maxTop) y = this.maxTop;

                    this.target.style.left = x + "px";
                    this.target.style.top = y + "px";
                }

            },
            dragEnd: function () {
                // 停止移动 -> 目标元素不在跟随鼠标移动
                document.onmouseup = function () {
                    document.onmousemove = null;
                    document.onmouseup = null;
                }
            },
        }

        // 调用dragEle时,自动获取编辑 开启拖拽
        obj.getBoundary();
        obj.dragStart();   // this-> dragEle

        return obj;
    }

    // 字面量对象 版本 => 拖拽一个元素
    var shadowList = document.getElementsByClassName("shadow");

    for (var i = 0; i < shadowList.length; i++) {
        var shadow = shadowList[i];
        var res = dragEle(shadow);
        console.log(res);
    }




</script>

</html>